package com.example.demo.dto.base;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 2025/10/26
 * wys
 */
@Getter
@Setter
public class R<T> implements Serializable {
    /**
     * 返回码 200-正确
     */
    private String code;
    /**
     * 数据
     */
    private T data;
    /**
     * 返回消息
     */
    private String msg;
    /**
     * 相应时间
     */
    private LocalDateTime dateTime;
    /**
     * 响应id
     */
    private String reqId;

    /**
     * 返回成功
     *
     * @param data data
     */
    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.setCode("200");
        r.setData(data);
        r.setDateTime(LocalDateTime.now());
        r.setReqId(UUID.randomUUID().toString().replaceAll("-", ""));
        return r;
    }

    /**
     * 返回错误
     * @param msg msg
     */
    public static <T> R<T> err(String msg) {
        R<T> r = new R<>();
        r.setCode("500");
        r.setMsg(msg);
        r.setDateTime(LocalDateTime.now());
        r.setReqId(UUID.randomUUID().toString().replaceAll("-", ""));
        return r;
    }
}
